<?php
/**
 * Admin utils
 *
 * @author Dima
 */
//var_dump(Zend_Auth::getInstance()->getIdentity());
class Lemar_Message
{
	
//----------------------------------------------------------------------------------------------------
	
	public static function previewMessage($idMes, $idCamp)
    {
    	$body 	 = Lemar_Message::getBody($idMes);
    	$subject = Lemar_Message::getSubject($idMes);
    	$from 	 = Lemar_Message::getFrom($idCamp);
    	
    	$aMess['body']		= $body; 
    	$aMess['subject']	= $subject; 
    	$aMess['from']		= $from;
    	
    	return $aMess;
    }

    
//----------------------------------------------------------------------------------------------------

	public static function getFrom($idCamp)
    {
    	$from = models_CampaignfromMapper::getRandom($idCamp);
    	return $from;
    }
        
//----------------------------------------------------------------------------------------------------

	public static function getSubject($idMes)
    {
    	$subject = models_MessagesubjectMapper::getRandom($idMes);
    	
    	if (! isset($subject->subject))
    	{
    		return false;
    	}
		$text = $subject->subject;
		
		$fOpen = strpos($text, '{~');
		if ($fOpen)
        {
			$text = Lemar_Message::repl($text);
		}
		$subject->subject = $text;
		
    	return $subject;
    }
        
    //----------------------------------------------------------------------------------------------------

	public static function getBody($idMes)
    {
    	$message = models_MessageMapper::findById($idMes);
		
    	if (! isset($message->body))
    	{
    		return false;
    	}
    	$body = $message->body;
    	
    	$fOpen = strpos($body, '{~');
        if ($fOpen)
        {
        	$body = Lemar_Message::repl($body);
		}
    	$message->body = $body;
    	
    	return $message;
    }
    
//----------------------------------------------------------------------------------------------------

    public static function repl($body)
    {
   		$fOpen = strpos($body, '{~');
        if ($fOpen)
        {
	    	$fClose = strpos($body, '~}');
	    	$leng 	= $fClose - $fOpen - 2;
	    	$subStr = substr($body, $fOpen + 2, $leng);
	    	$aVar 	= explode('~|~', $subStr);
	    	$randKeys 	= array_rand($aVar);
	    	$needStr 	= $aVar[$randKeys];
	    	$replStr 	= substr($body, $fOpen, $leng + 4);
	    	$body 		= str_replace($replStr, $needStr, $body);
        }
   		$fOpen = strpos($body, '{~');
        if ($fOpen)
        {
        	$body = Lemar_Message::repl($body);
        }
		return $body;
    }
    
	
//----------------------------------------------------------------------------------------------------

	
	public static function getRandomByIdCampaign($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		
		$select  = $db->select();
		$select->from('message', array('id', 'id_campaign', 'body'))
	  			->join('message_subject', 'message_subject.id_message = message.id', '')
	  			->where('id_campaign = ?', $id)
	  			->order(new Zend_Db_Expr('RAND()'));
	  	
	  	$result = $db->fetchRow($select);
	  	
	  	return $result;
	}
	
	/**
	 * Returns an array
	 * 
	 * @param int $idCamp Optionally Campaign Id.
	 * 
	 * @return array $counts An array of $idCamp => $count
	 */
	public static function getCountByIdCampaign( $idCamp = null )
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		
		$select  = $db->select();
		$select ->from('message', array('id_campaign', 'COUNT(*) AS mess_count'))
	  			->join('message_subject', 'message_subject.id_message = message.id')
	  			->group('id_campaign');
	  	if ($idCamp)
	  	{
	  		$select->where('id_campaign = ?', $idCamp);
	  	}
	  	
	  	$result = $db->fetchAll($select);
	  	
	  	foreach ($result as $k => $v)
	  	{
	  		$counts[$v['id_campaign']] = $v['mess_count'];
	  	}
		
	  	return $counts;
	}
    
//----------------------------------------------------------------------------------------------------
    
}